{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "mtb = dict()\n",
    "rmtb = dict()\n",
    "\n",
    "mtb = {\n",
    "'A': '·-',\n",
    "'B': '-···',\n",
    "'C': '-·-·',\n",
    "'D': '-··',\n",
    "'E': '·',\n",
    "'F': '··-·',\n",
    "'G': '--·',\n",
    "'H': '····',\n",
    "'I': '··',\n",
    "'J': '·---',\n",
    "'K': '-·-',\n",
    "'L': '·-··',\n",
    "'M': '--',\n",
    "'N': '-·',\n",
    "'O': '---',\n",
    "'P': '·--·',\n",
    "'Q': '--·-',\n",
    "'R': '·-·',\n",
    "'S': '···',\n",
    "'T': '-',\n",
    "'U': '··-',\n",
    "'V': '···-',\n",
    "'W': '·--',\n",
    "'X': '-··-',\n",
    "'Y': '-·--',\n",
    "'Z': '--··',\n",
    "'1': '·----',\n",
    "'2': '··---',\n",
    "'3': '···--',\n",
    "'4': '····',\n",
    "'5': '·····',\n",
    "'6': '-····',\n",
    "'7': '--···',\n",
    "'8': '---··',\n",
    "'9': '----·',\n",
    "'0': '-----',\n",
    "'.': '·-·-·-',\n",
    "':': '---···',\n",
    "',': '--··--',\n",
    "';': '-·-·-·',\n",
    "'?': '··--··',\n",
    "'=': '-···-',\n",
    "'\\'': '·----·',\n",
    "'/': '-··-·',\n",
    "'!': '-·-·--',\n",
    "'-': '-····-',\n",
    "'_': '··--·-',\n",
    "'\"': '·-··-·',\n",
    "'(': '-·--·',\n",
    "')': '-·--·-',\n",
    "'$': '···-··-',\n",
    "'&': '·-···',\n",
    "'@': '·--·-·',\n",
    "'+': '·-·-·',\n",
    "}\n",
    "\n",
    "rmtb = {'·-': 'A',\n",
    " '-···': 'B',\n",
    " '-·-·': 'C',\n",
    " '-··': 'D',\n",
    " '·': 'E',\n",
    " '··-·': 'F',\n",
    " '--·': 'G',\n",
    " '····': 'H',\n",
    " '··': 'I',\n",
    " '·---': 'J',\n",
    " '-·-': 'K',\n",
    " '·-··': 'L',\n",
    " '--': 'M',\n",
    " '-·': 'N',\n",
    " '---': 'O',\n",
    " '·--·': 'P',\n",
    " '--·-': 'Q',\n",
    " '·-·': 'R',\n",
    " '···': 'S',\n",
    " '-': 'T',\n",
    " '··-': 'U',\n",
    " '···-': 'V',\n",
    " '·--': 'W',\n",
    " '-··-': 'X',\n",
    " '-·--': 'Y',\n",
    " '--··': 'Z',\n",
    " '·----': '1',\n",
    " '··---': '2',\n",
    " '···--': '3',\n",
    " '····-': '4',\n",
    " '·····': '5',\n",
    " '-····': '6',\n",
    " '--···': '7',\n",
    " '---··': '8',\n",
    " '----·': '9',\n",
    " '-----': '0',\n",
    " '·-·-·-': '.',\n",
    " '---···': ':',\n",
    " '--··--': ',',\n",
    " '-·-·-·': ';',\n",
    " '··--··': '?',\n",
    " '-···-': '=',\n",
    " '·----·': \"'\",\n",
    " '-··-·': '/',\n",
    " '-·-·--': '!',\n",
    " '-····-': '-',\n",
    " '··--·-': '_',\n",
    " '·-··-·': '\"',\n",
    " '-·--·': '(',\n",
    " '-·--·-': ')',\n",
    " '···-··-': '$',\n",
    " '·-···': '&',\n",
    " '·--·-·': '@',\n",
    " '·-·-·': '+'\n",
    " }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def morse_encode(src_text):\n",
    "    res = ''\n",
    "    src_text = src_text.upper()\n",
    "    for ch in src_text:\n",
    "        res += mtb.get(ch, '\\\\') + ' '\n",
    "    return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "'·· \\\\ ·-·· --- ···- · \\\\ ··- -·-·-- '"
     },
     "metadata": {},
     "execution_count": 3
    }
   ],
   "source": [
    "morse_encode('I love u!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def morse_decode(morse_text):\n",
    "    res = ''\n",
    "    for ch in morse_text.split():\n",
    "        res += rmtb.get(ch, ' ') + ' '\n",
    "    return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "'I   L O V E   U ! '"
     },
     "metadata": {},
     "execution_count": 5
    }
   ],
   "source": [
    "morse_decode('·· \\\\ ·-·· --- ···- · \\\\ ··- -·-·-- ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "class morseCodec:\n",
    "    def __init__(self):\n",
    "        self.mtb = dict()\n",
    "        self.rmtb = dict()\n",
    "        self.mtb = {\n",
    "        'A': '·-',\n",
    "        'B': '-···',\n",
    "        'C': '-·-·',\n",
    "        'D': '-··',\n",
    "        'E': '·',\n",
    "        'F': '··-·',\n",
    "        'G': '--·',\n",
    "        'H': '····',\n",
    "        'I': '··',\n",
    "        'J': '·---',\n",
    "        'K': '-·-',\n",
    "        'L': '·-··',\n",
    "        'M': '--',\n",
    "        'N': '-·',\n",
    "        'O': '---',\n",
    "        'P': '·--·',\n",
    "        'Q': '--·-',\n",
    "        'R': '·-·',\n",
    "        'S': '···',\n",
    "        'T': '-',\n",
    "        'U': '··-',\n",
    "        'V': '···-',\n",
    "        'W': '·--',\n",
    "        'X': '-··-',\n",
    "        'Y': '-·--',\n",
    "        'Z': '--··',\n",
    "        '1': '·----',\n",
    "        '2': '··---',\n",
    "        '3': '···--',\n",
    "        '4': '····',\n",
    "        '5': '·····',\n",
    "        '6': '-····',\n",
    "        '7': '--···',\n",
    "        '8': '---··',\n",
    "        '9': '----·',\n",
    "        '0': '-----',\n",
    "        '.': '·-·-·-',\n",
    "        ':': '---···',\n",
    "        ',': '--··--',\n",
    "        ';': '-·-·-·',\n",
    "        '?': '··--··',\n",
    "        '=': '-···-',\n",
    "        '\\'': '·----·',\n",
    "        '/': '-··-·',\n",
    "        '!': '-·-·--',\n",
    "        '-': '-····-',\n",
    "        '_': '··--·-',\n",
    "        '\"': '·-··-·',\n",
    "        '(': '-·--·',\n",
    "        ')': '-·--·-',\n",
    "        '$': '···-··-',\n",
    "        '&': '·-···',\n",
    "        '@': '·--·-·',\n",
    "        '+': '·-·-·',\n",
    "        }\n",
    "\n",
    "        self.rmtb = {'·-': 'A',\n",
    "        '-···': 'B',\n",
    "        '-·-·': 'C',\n",
    "        '-··': 'D',\n",
    "        '·': 'E',\n",
    "        '··-·': 'F',\n",
    "        '--·': 'G',\n",
    "        '····': 'H',\n",
    "        '··': 'I',\n",
    "        '·---': 'J',\n",
    "        '-·-': 'K',\n",
    "        '·-··': 'L',\n",
    "        '--': 'M',\n",
    "        '-·': 'N',\n",
    "        '---': 'O',\n",
    "        '·--·': 'P',\n",
    "        '--·-': 'Q',\n",
    "        '·-·': 'R',\n",
    "        '···': 'S',\n",
    "        '-': 'T',\n",
    "        '··-': 'U',\n",
    "        '···-': 'V',\n",
    "        '·--': 'W',\n",
    "        '-··-': 'X',\n",
    "        '-·--': 'Y',\n",
    "        '--··': 'Z',\n",
    "        '·----': '1',\n",
    "        '··---': '2',\n",
    "        '···--': '3',\n",
    "        '····-': '4',\n",
    "        '·····': '5',\n",
    "        '-····': '6',\n",
    "        '--···': '7',\n",
    "        '---··': '8',\n",
    "        '----·': '9',\n",
    "        '-----': '0',\n",
    "        '·-·-·-': '.',\n",
    "        '---···': ':',\n",
    "        '--··--': ',',\n",
    "        '-·-·-·': ';',\n",
    "        '··--··': '?',\n",
    "        '-···-': '=',\n",
    "        '·----·': \"'\",\n",
    "        '-··-·': '/',\n",
    "        '-·-·--': '!',\n",
    "        '-····-': '-',\n",
    "        '··--·-': '_',\n",
    "        '·-··-·': '\"',\n",
    "        '-·--·': '(',\n",
    "        '-·--·-': ')',\n",
    "        '···-··-': '$',\n",
    "        '·-···': '&',\n",
    "        '·--·-·': '@',\n",
    "        '·-·-·': '+'\n",
    "        }\n",
    "\n",
    "    def encode(self, src_text):\n",
    "        res = ''\n",
    "        src_text = src_text.upper()\n",
    "        for ch in src_text:\n",
    "            res += self.mtb.get(ch, '\\\\') + ' '\n",
    "        return res\n",
    "    \n",
    "    def decode(self, morse_text):\n",
    "        res = ''\n",
    "        for seg in morse_text.split():\n",
    "            res += self.rmtb.get(seg, ' ') + ' '\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "'·· \\\\ ·-·· --- ···- · \\\\ ··- -·-·-- '"
     },
     "metadata": {},
     "execution_count": 7
    }
   ],
   "source": [
    "mytest = morseCodec()\n",
    "mytest.encode('I love u!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "'I   L O V E   U ! '"
     },
     "metadata": {},
     "execution_count": 8
    }
   ],
   "source": [
    "mytest.decode('·· \\\\ ·-·· --- ···- · \\\\ ··- -·-·-- ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "'··\\\\·-··---···-·\\\\··--·-·--'"
     },
     "metadata": {},
     "execution_count": 9
    }
   ],
   "source": [
    "'·· \\\\ ·-·· --- ···- · \\\\ ··- -·-·-- '.replace(\" \", '')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "max_n = max(len(k) for k in rmtb)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "def guess_morse(seg, cur_morse, cur_start, res):\n",
    "    for end in range(cur_start + 1, min(cur_start + max_n + 1, len(seg) + 1)):\n",
    "        single_char = rmtb.get(seg[cur_start: end], None)\n",
    "        if single_char:\n",
    "            if end == len(seg):\n",
    "                res.append(cur_morse + single_char)\n",
    "                break\n",
    "            else:\n",
    "                guess_morse(seg, cur_morse + single_char, end, res)\n",
    "    if cur_start == 0:\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "['EEEE', 'EEI', 'EIE', 'ES', 'IEE', 'II', 'SE', 'H']"
     },
     "metadata": {},
     "execution_count": 12
    }
   ],
   "source": [
    "guess_morse('····', '', 0, [])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}